package tema13.ejercicio14;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Ej14 extends JFrame {
	private String titulo = "Calculadora Java";
	private final int ancho = 350;
	private final int alto = 500;
	private int anchoEscritorio, altoEscritorio, x, y;
	protected long numero1, numero2, sol;
	protected char operador;
	protected int estado;

	private JPanel l1, l2, l3, l4, l5, jp;
	private JButton boton7, boton8, boton9, botonmas;
	private JButton boton4, boton5, boton6, botonmenos;
	private JButton boton1, boton2, boton3, botonpor;
	private JButton botonc, boton0, botonigual, botondividir;

	protected JLabel mensaje;
	private MetodosCalculadora metodos;

	private Ej14() {
		inicializar();
		inicializarUbicacion();
		inicializarPanel();
		setResizable(false);
		setVisible(true);
	}

	private void inicializarPanel() {
		mensaje = new JLabel();
		mensaje.setFont(new Font("Arial", Font.BOLD, 20));

		l1 = new JPanel(new GridLayout(1, 4));
		l2 = new JPanel(new GridLayout(1, 4));
		l3 = new JPanel(new GridLayout(1, 4));
		l4 = new JPanel(new GridLayout(1, 4));
		l5 = new JPanel(new GridLayout(1, 4));

		boton7 = new JButton("7");
		boton7.setFont(new Font("", Font.BOLD, 25));
		boton7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionDigito('7');
			}
		});
		l1.add(boton7);

		boton8 = new JButton("8");
		boton8.setFont(new Font("", Font.BOLD, 25));
		boton8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionDigito('8');
			}
		});
		l1.add(boton8);

		boton9 = new JButton("9");
		boton9.setFont(new Font("", Font.BOLD, 25));
		boton9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionDigito('9');
			}
		});
		l1.add(boton9);

		botonmas = new JButton("+");
		botonmas.setFont(new Font("", Font.BOLD, 25));
		botonmas.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionOperando('+');
			}
		});
		l1.add(botonmas);

		boton4 = new JButton("4");
		boton4.setFont(new Font("", Font.BOLD, 25));
		boton4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionDigito('4');
			}
		});
		l2.add(boton4);

		boton5 = new JButton("5");
		boton5.setFont(new Font("", Font.BOLD, 25));
		boton5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionDigito('5');
			}
		});
		l2.add(boton5);

		boton6 = new JButton("6");
		boton6.setFont(new Font("", Font.BOLD, 25));
		boton6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionDigito('6');
			}
		});
		l2.add(boton6);

		botonmenos = new JButton("-");
		botonmenos.setFont(new Font("", Font.BOLD, 25));
		botonmenos.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionOperando('-');
			}
		});
		l2.add(botonmenos);

		boton1 = new JButton("1");
		boton1.setFont(new Font("", Font.BOLD, 25));
		boton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionDigito('1');
			}
		});
		l3.add(boton1);

		boton2 = new JButton("2");
		boton2.setFont(new Font("", Font.BOLD, 25));
		boton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionDigito('2');
			}
		});
		l3.add(boton2);

		boton3 = new JButton("3");
		boton3.setFont(new Font("", Font.BOLD, 25));
		boton3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionDigito('3');
			}
		});
		l3.add(boton3);

		botonpor = new JButton("X");
		botonpor.setFont(new Font("", Font.BOLD, 25));
		botonpor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionOperando('X');
			}
		});
		l3.add(botonpor);

		botonc = new JButton("C");
		botonc.setFont(new Font("", Font.BOLD, 25));
		botonc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionOperando('C');
			}
		});
		l4.add(botonc);

		boton0 = new JButton("0");
		boton0.setFont(new Font("", Font.BOLD, 25));
		boton0.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionDigito('0');
			}
		});
		l4.add(boton0);

		botonigual = new JButton("=");
		botonigual.setFont(new Font("", Font.BOLD, 25));
		botonigual.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionOperando('=');
			}
		});
		l4.add(botonigual);

		botondividir = new JButton("/");
		botondividir.setFont(new Font("", Font.BOLD, 25));
		botondividir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodos.accionOperando('/');
			}
		});
		l4.add(botondividir);

		jp.add(mensaje);
		jp.add(l1);
		jp.add(l2);
		jp.add(l3);
		jp.add(l4);

	}

	private void inicializar() {
		setTitle(titulo);
		setSize(ancho, alto);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		jp = new JPanel();
		jp.setLayout(new GridLayout(5, 1));
		this.add(jp);
		this.metodos = new MetodosCalculadora(this);

		numero1 = 0;
		numero2 = 0;
		estado = 1;
	}

	private void inicializarUbicacion() {
		altoEscritorio = Toolkit.getDefaultToolkit().getScreenSize().height;
		anchoEscritorio = Toolkit.getDefaultToolkit().getScreenSize().width;
		y = (altoEscritorio / 2) - (alto / 2);
		x = (anchoEscritorio / 2) - (ancho / 2);
		setLocation(x, y);
	}

	public static void main(String[] args) {
		new Ej14();
	}

}
